<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '轮播图添加' : '轮播图修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      {{ user }}
      <el-form :model="user">
        <el-form-item label="标题" label-width="120px">
            <el-input v-model="user.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="120px">
          <div class="pro">
            <h3>+</h3>
            <img :src="imgUrl" v-if="imgUrl">
            <input type="file"  @change="getFile"/>
          </div>
        </el-form-item>
        
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <!-- </el-form> -->

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import path from "path";
import { successAlert, errorAlert } from "../../../utils";
import { reqbanneradd, reqspecsadd,reqbannerinfo, reqbanneredit } from '../../../http/api';
export default {
  props: ["info"],
  methods: {
    add(){
        reqbanneradd(this.user).then(res => {
            successAlert(res.data.msg);
            this.info.isshow = false;
            this.empty();
            this.$emit('init')
        })
    },
    cancel(){
        this.info.isshow = false;
        this.empty();
    },
    update(){
        reqbanneredit(this.user).then(res => {
            successAlert(res.data.msg);
            this.$emit('init');
            this.empty();
            this.info.isshow = false;
        })
    },
    empty(){
        this.user = {
         title:'',
        img:'',
        status:1
        },
        this.imgUrl = '';
    },
    getFile(e) {
      let file = e.target.files[0];
      //验证大小 file.size 单位是B    1M=1024KB 1KB=1024B 1B=8b
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件最大2M");
        return;
      }

      let imgArr = [".png", ".jpeg", ".jpg", ".gif"];
      let extname = path.extname(file.name); // .png
      if (!imgArr.includes(extname)) {
        errorAlert("请上传正确的图片格式");
        return;
      }
      // 生成图片地址
      this.imgUrl = URL.createObjectURL(file)
      //存到user，后续发送给后端
      this.user.img=file;
        console.log(this.user.img);
    },
     getOne(id){
        reqbannerinfo({id}).then(res => {
            this.user = res.data.list;
            this.user.id = id;
            this.imgUrl = this.$pre + this.user.img;
        })
    },
    close(){
        if(!this.info.isAdd){
            this.empty()
        }
    }
  },
  mounted() {
   
  },

  data() {
    return {
      user:{
        title:'',
        img:'',
        status:1
      },
      imgUrl:''
    };
  },
};
</script>

<style scoped >
.pro {
  width: 200px;
  height: 200px;
  border: 1px solid #000;
  position: relative;
}
.pro input {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
  opacity: 0;
}
.pro img{
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}
.pro h3 {
  text-align: center;
  line-height: 200px;
}

</style>